package com.ymatou.iapi.productsync.task;

import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.productquery.compare.LoggerHelper;
import com.ymatou.iapi.productsync.service.EsCall;
import com.ymatou.iapi.productsync.service.MongocrudCallOld;
import com.ymttest.utils.FileUtil;
import com.ymttest.utils.YMTDateUtil;

public class CallSync {
	private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, Long.MAX_VALUE,
			TimeUnit.SECONDS, new LinkedBlockingQueue<>());

	public static void runTask(List<Runnable> runList) {
		Date startDate = YMTDateUtil.getDate();

		runList.forEach(ele -> {
			threadPoolExecutor.execute(ele);
		});

		threadPoolExecutor.shutdown();// 只是不能再提交新任务，等待执行的任务不受影响

		try {
			boolean loop = true;
			do { // 等待所有任务完成
				loop = !threadPoolExecutor.awaitTermination(1, TimeUnit.SECONDS); // 阻塞，直到线程池里所有任务结束
			} while (loop);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		Date endDate = YMTDateUtil.getDate();
		System.out.println((endDate.getTime() - startDate.getTime()) / 1000);
		System.out.println(endDate);
	}

	public static void callSyc_new(int activityId, String productId, String actionType) {
		JSONObject newRet = new MongocrudCallOld().callAndGetReturnData(activityId, productId, actionType);
		System.out.println(
				"调[Mongo]同步服务 ProductId:" + productId + ", ActivityId:" + activityId + ", ActionType:" + actionType);
		try {
			LoggerHelper.writeResult(LoggerHelper.syclogPath, "ProductId:" + productId + ", ActivityId:" + activityId
					+ ", ActionType:" + actionType + ",Mongo同步返回:" + newRet.getBoolean("success"), true);
		} catch (JSONException e) {
			LoggerHelper.failure("调[Mongo]同步服务失败,ProductId:" + productId + ",activityId:" + activityId + ",actionType:"
					+ actionType + ",error" + e.getMessage(), true);
		}
	}

	public static void callSycES_new(int activityId, String productId, String actionType) {
		JSONObject newRet = new EsCall().callAndGetReturnData(activityId, productId, actionType);
		System.out.println(
				"调[ES]同步服务 ProductId:" + productId + ", ActivityId:" + activityId + ", ActionType:" + actionType);
		try {
			LoggerHelper.writeResult(LoggerHelper.syclogPath, "ProductId:" + productId + ", ActivityId:" + activityId
					+ ", ActionType:" + actionType + ",ES同步返回:" + newRet.getString("response"), true);
		} catch (JSONException e) {
			LoggerHelper.failure("调[ES]同步服务失败,ProductId:" + productId + ",activityId:" + activityId + ",actionType:"
					+ actionType + ",error" + e.getMessage(), true);
		}
	}

	
	
	public static void main(String[] args) {

		String actionType = args.length == 0 ? "AddProduct" : args[0];

		LoggerHelper.setfolderPath(".");
		FileUtil.deleteFile(LoggerHelper.syclogPath);
		FileUtil.deleteFile(LoggerHelper.errorPath);

		String prodPath = "sync_aid_pid.txt";
		List<String> aid_pids = FileUtil.readFileAsLine(prodPath);

		for (int i = 0; i < aid_pids.size(); i++) {
			String[] param = aid_pids.get(i).split(",");
			if (param.length == 2) {

				// mongo
				callSyc_new(Integer.parseInt(param[0]), param[1].toString(), actionType);

				// ES
				callSycES_new(Integer.parseInt(param[0]), param[1].toString(), actionType);

				if (actionType.equalsIgnoreCase("DeleteProduct")) {
					YMTDateUtil.waitTime(0.05);
				} else {
					YMTDateUtil.waitTime(0.005);
				}

			} else {
				LoggerHelper.failure("参数有误" + aid_pids.get(i), true);
			}

		}

	}

}
